#include "objReader.h"

void Tokenize(const string& str,
			  vector<string>& tokens,
			  const string& delimiters)
{
	// Skip delimiters at beginning.
	string::size_type lastPos = str.find_first_not_of(delimiters, 0);
	// Find first "non-delimiter".
	string::size_type pos     = str.find_first_of(delimiters, lastPos);

	while (string::npos != pos || string::npos != lastPos)
	{
		// Found a token, add it to the vector.
		tokens.push_back(str.substr(lastPos, pos - lastPos));
		// Skip delimiters.  Note the "not_of"
		lastPos = str.find_first_not_of(delimiters, pos);
		// Find next "non-delimiter"
		pos = str.find_first_of(delimiters, lastPos);
	}
}

face* readFace(string fac1,string fac2,string fac3, vector<vec3*> verts, vector<vec2*> texts, vector<vec3*> norms){

	vector<string> vec;
	Tokenize(fac1,vec,"/");
	face *fac=new face();
	for(int i=0;i<(int)vec.size();i++){
		if(i==0){//v
			int ind=atoi(vec[i].c_str());
			fac->v1=ind-1;
		}
		if(i==1){//t
			if(!vec[i].empty()){
				int ind=atoi(vec[i].c_str());
				fac->t1=ind-1;
			}
		}
		if(i==2){//n
			int ind=atoi(vec[i].c_str());
			fac->n1=ind-1;
		}
	}

	vec.clear();
	Tokenize(fac2,vec,"/");
	for(int i=0;i<(int)vec.size();i++){
		if(i==0){//v
			int ind=atoi(vec[i].c_str());
			fac->v2=ind-1;
		}
		if(i==1){//t
			if(!vec[i].empty()){
				int ind=atoi(vec[i].c_str());
				fac->t2=ind-1;
			}
		}
		if(i==2){//n
			int ind=atoi(vec[i].c_str());
			fac->n2=ind-1;
		}
	}

	vec.clear();
	Tokenize(fac3,vec,"/");

	for(unsigned int i=0;i<vec.size();i++){
		if(i==0){//v
			int ind=atoi(vec[i].c_str());
			fac->v3=ind-1;
		}
		if(i==1){//t
			if(!vec[i].empty()){
				int ind=atoi(vec[i].c_str());
				fac->t3=ind-1;
			}
		}
		if(i==2){//n
			int ind=atoi(vec[i].c_str());
			fac->n3=ind-1;
		}

	}
	return fac;
}




obj3d * loadOBJ(const string file){
	string line;
	//LOG("comienza carga %s", file.c_str());
	ifstream myfile (file.c_str());
	if (myfile.is_open())
	{
		LOG("file open");
		vector<vec3*> verts,normals;
		vector<vec2*> texts;
		vector<face*> faces;
		while ( myfile.good() )
		{

			getline (myfile,line);
			//cout << line << endl;

			if(!line.empty() && line[0]!='#'){
				vector<string> spl;
				Tokenize(line,spl);
				string p1=spl[0];
				if(p1=="v"){
					float x=(float)atof(spl[1].c_str());
					float y=(float)atof(spl[2].c_str());
					float z=(float)atof(spl[3].c_str());
					vec3 *vert=new vec3(x,y,z);
					verts.push_back(vert);
				}
				else if(p1=="vt"){
					float x=(float)atof(spl[1].c_str());float y=(float)atof(spl[2].c_str());
					vec2 *vert=new vec2(x,y);
					texts.push_back(vert);
				}
				else if(p1=="vn"){
					float x=(float)atof(spl[1].c_str());float y=(float)atof(spl[2].c_str());float z=(float)atof(spl[3].c_str());
					vec3 *vert=new vec3(x,y,z);
					normals.push_back(vert);
				}
				else if(p1=="f"){
					face* fac=readFace(spl[1],spl[2],spl[3],verts,texts,normals);
					faces.push_back(fac);
				}

			}
		}



		myfile.close();
		obj3d* obj= new obj3d(faces,verts,texts, normals);
		//LOG("termina carga..");
		//LOG("%d", obj->numVerts);
		return obj;
	}

	return NULL;

}
